import React from 'react';
import { Button, Modal, message } from 'antd';
console.info('ActionButton');
type ActionButtonProps<T extends React.ElementType> = {
  tag?: T;
  tooltip?: string|React.ReactNode
  action: Function,
  refresh?: Function,
  successMessage?:string
  // data: any,
  children: React.ReactNode;
} & React.ComponentProps<T>; // 使用 React.ComponentProps 获取传入组件的 props
const msg_key = 'action_button_loading'
const ActionButton = <T extends React.ElementType = typeof Button>({
  tag: Tag = Button as unknown as T,
  children,
  successMessage,
  tooltip,
  // data,
  refresh,
  action,
  ...props
}: ActionButtonProps<T>) => {
  const _action = async () => {
    message.loading({
      key: msg_key,
      content: 'loading...'
    })
    try {
      await action()
      refresh&&refresh();
      if(typeof successMessage !='undefined'){
        if(successMessage){

          message.success(successMessage);
        }
      }else{
        message.success('操作成功');
      }
    } catch (error) {
      // message.error('错误');
    }

    message.destroy(msg_key)
  }
  const onClick = async () => {

    if (tooltip) {
      Modal.confirm({
        title: '温馨提示',
        content: tooltip,
        async onOk(...args) {
          await _action()
        }
      })
    } else {
      _action()
    }
  }

  return <Tag onClick={onClick} {...props}>{children}</Tag>
}
export default ActionButton